---@class RedRob.EffectMgr
local EffectMgr = RedRob.CreateSingleton("EffectMgr")
local this = EffectMgr
function EffectMgr.OnCreate()
    ---@type RedRob.EffectBase[]
    this.effectList = {}
    ---@type RedRob.EffectBase[]
    this.tempList = {}
    this.staticUniqueId = 0
    ---@type RedRob.EffectBase[]
    this.removedList = {}
    RedRob.ClientBattle.RegisterUpdateCallback(this.Update)
end
function EffectMgr.OnDestroy()
    if this.effectList then
        for k,v in pairs(this.effectList) do
            v:OnDestroy()
        end
    end
    this.tempList = nil
    this.removedList = nil
end
function EffectMgr.GetUniqueId()
    this.staticUniqueId = this.staticUniqueId + 1
    return this.staticUniqueId
end
function EffectMgr.GetPos(originX,originY,offsetX,offsetY,isLeft)
    if isLeft then
        offsetX = -offsetX
    end
    local x = originX + offsetX
    local y = originY + offsetY
    return x,y
end
---@param effect RedRob.EffectBase
function EffectMgr.UpdateEffect(effect,casterId,targetId,skillId,resName,actionName,x,y,during,sortingOrder,followId)
    effect.casterId = casterId or 0
    effect.targetId = targetId or 0
    effect.animName = actionName
    effect.resName = resName
    effect.skillId = skillId or 0
    effect.pos:Set(x,y)
    effect.during = during or -1
    effect.sortingOrder = sortingOrder
    effect.followId = followId or 0
end
---@return RedRob.EffectBase
function EffectMgr.CreateEffect(casterId,resName,actionName,x,y,during,sortingOrder,followId)
    local effect = RedRob.PoolMgr.GetEffectFromPool(this.GetUniqueId(),RedRob.Config.EffectType.effect)
    --local effect = RedRob.EffectBase.New(this.GetUniqueId(),RedRob.Config.EffectType.effect)
    effect:OnCreate()
    this.UpdateEffect(effect,casterId,0,0,resName,actionName,x,y,during,sortingOrder,followId)
    this.AddEffect(effect)
    effect:Play()
    return effect
end
---@return RedRob.Bullet
function EffectMgr.CreateBullet(casterId,targetId,skillId,resName,actionName,x,y,during,sortingOrder)
    local effect = RedRob.PoolMgr.GetEffectFromPool(this.GetUniqueId(),RedRob.Config.EffectType.bullet)
    --local effect = RedRob.Bullet.New(this.GetUniqueId(),RedRob.Config.EffectType.bullet)
    effect:OnCreate()
    this.UpdateEffect(effect,casterId,targetId,skillId,resName,actionName,x,y,during,sortingOrder)
    this.AddEffect(effect)
    effect:Play()
    return effect
end
---@private
---@param effect RedRob.EffectBase
function EffectMgr.EffectToPool(effect)
    effect:OnDestroy()
    RedRob.PoolMgr.ReturnEffectToPool(effect)
end
function EffectMgr.DestroyEffect(effect)
    if not effect then return end
    this.effectList[effect.id] = nil
    this.EffectToPool(effect)
end
function EffectMgr.DestroyEffectByCaster(casterId)
    for k,v in pairs(this.effectList) do
        if v.casterId == casterId then
           v:Die()
        end
    end
end
function EffectMgr.DestroyEffectBySkill(skillId)
    for k,v in pairs(this.effectList) do
        if v.skillId == skillId then
            v:Die()
        end
    end
end
---@param effect RedRob.EffectBase
function EffectMgr.AddEffect(effect)
    this.effectList[effect.id] = effect
end
function EffectMgr.Update(dt)
    table.clear(this.removedList)
    table.clear(this.tempList)
    for k,v in pairs(this.effectList) do
        this.tempList[k] = v
    end
    for k,v in pairs(this.tempList) do
        v:Update(dt)
        if v:IsDie() then
            table.insert(this.removedList,v)
        end
    end
    --
    for k,v in pairs(this.removedList) do
        this.DestroyEffect(v)
    end
end